package repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import domain.AllProperty;

public class AllPropertyDAO extends BaseDAO {

	private static final long serialVersionUID = -276853828726825109L;

	public AllPropertyDAO() throws SQLException {
		super();
	}

	@Override
	public ResultSet query(Object... params) {
		String sql = "SELECT * FROM 总资产表 WHERE 物品ID=?;";
		ResultSet rs = null;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			rs = pst.executeQuery();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet queryAll() {
		String sql = "SELECT * FROM 总资产表;";
		ResultSet rs = null;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);			
			rs = pst.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	@Override
	public int update(Object... params) {
		String sql = "UPDATE 总资产表 SET 物品名称=?,物品价格=?,所属学院=?,使用状况=?,数量=? WHERE 物品ID=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			AllProperty allpro = (AllProperty)params[0];
			pst.setString(6, allpro.getID());
			pst.setString(1, allpro.getName());
			pst.setString(2, allpro.getPrice());
			pst.setString(3, allpro.getLocation());
			pst.setString(4, allpro.getUse());
			pst.setString(5, allpro.getNumber());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(Object... params) {
		String sql = "DELETE FROM 总资产表 WHERE 物品ID=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int insert(Object... params) {
		String sql = "INSERT INTO 总资产表(物品ID,物品名称,物品价格,所属学院,使用状况,数量)"+"VALUES(?,?,?,?,?,?);";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			AllProperty acad = (AllProperty)params[0];
			pst.setString(1, acad.getID());
			pst.setString(2, acad.getName());
			pst.setString(3, acad.getPrice());
			pst.setString(4, acad.getLocation());
			pst.setString(5, acad.getUse());
			pst.setString(6, acad.getNumber());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

}
